package com.lottery.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.lottery.dao.pc28.lobbyDao.LotteryDao;
import com.lottery.service.pc28.lobbyService.LotteryDrawService;
import com.lottery.service.qiuqiu.JedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.entity.Lottery;

//从网络接口中获取开奖信息,存入数据库
public class GetData {
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private LotteryDao lotteryDao;
	@Autowired
	private LotteryDrawService lotteryDrawService;
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	public void insert() {
		try {
			String httpRequest = HttpRequestUtil.httpRequest("http://www.kuaicaile.com/shishicai/getCqsscAwardData");
			//logger.info("httpRequest数据 是={}", httpRequest);
			// 转为JSON数据
			JSONObject parseObject = JSON.parseObject(httpRequest);
			//logger.info("JSON数据 是={}", parseObject);
			// 下一期信息
			JSONObject nextObject = parseObject.getJSONObject("next");

			// 当前信息
			JSONObject jsonObject = parseObject.getJSONObject("current");
			//logger.info("当前信息 是={}", jsonObject);
			// 当前开奖的期号
			String expect = String.valueOf(jsonObject.getLongValue("fullPeriodNumber"));
			//logger.info("当前开奖的期号 是={}", expect);
			// 从redis中查询上一期的开奖信息
			String expectRedis = jedisClient.get("getdata");
			// 如果本次获取的开奖信息和上次相同,就结束本次方法
			if (expect.equals(expectRedis)) {
				return;
			} else {
				// 如果本次获取的开奖信息和上次不同,就将本次的开奖信息存入数据库并跟新redis中的内容
				int expectNum = jsonObject.getIntValue("periodNumber");
				String opencode = jsonObject.getString("awardNumbers");
				Date opentime = jsonObject.getDate("awardTime");
				Date inserttime = new Date();
				
				// 查看数据库中是否有该期号的数据,如果有,就结束本次循环
				Lottery selectLotteryByExpect = lotteryDao.selectLotteryByExpect(expect);
				if (selectLotteryByExpect != null) {
					//logger.info("信息:{}", "数据库中有该数据,结束本次循环");
					return;
				}

				Lottery lottery = new Lottery();
				lottery.setExpect(expect);
				lottery.setExpectNum(expectNum);
				lottery.setOpencode(opencode);
				lottery.setOpentime(opentime);
				lottery.setInserttime(inserttime);
				
				
				jedisClient.set("getdata", expect);
				jedisClient.set("opencode", opencode);
				jedisClient.set("openDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(opentime));

				// 下次开奖的时间
				String nextTime = nextObject.getString("awardTime");
				jedisClient.set("nextTime", nextTime);

				// 下次开奖的期号
				String nextExpect = "";
				String nextExpectNum = nextObject.getString("periodNumber");
				String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
				if (nextExpectNum.length() == 1) {
					nextExpect = format + "0" + "0" + nextExpectNum;
				} else if (nextExpectNum.length() == 2) {
					nextExpect = format + "0" + nextExpectNum;
				} else {
					nextExpect = format + nextExpectNum;
				}
				jedisClient.set("nextExpect", nextExpect);
				//logger.info("开奖期号加号码 是={}", expect+"||||||"+opencode);
				
				lotteryDao.insertLottery(lottery);
				/****************测试默认查pc28****************/
				lotteryDrawService.pc28YesWinning(opencode, expect,2);

			}
		} catch (Exception e) {
			// 获取数据为乱码时,转为JSON数据会报异常
			////logger.info("获取数据为乱码时:{}", e);
			//logger.info("数据获取为乱码是={}", "重新获取");
			return;
		}
	}

}
